package com.briup.sms.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

/**
 * @Author :xuyang
 * @DateTime:2024/1/10 13:27
 * @Version :v1.0.0
 * ClassName:JWTUtils
 * Package  :com.briup.sms.utils
 * Description:TODO
 */
public class JWTUtils {
    private static final String sign = "briup";
    private static final long expiredTime = 1000 * 60 * 60 * 60;


    public static String generatorToken(Map<String, Object> map) {
        return Jwts.builder()
                .addClaims(map)
                .setExpiration(new Date(System.currentTimeMillis() + expiredTime))
                .signWith(SignatureAlgorithm.HS256, sign)
                .compact();
    }

    public static String generatorToken(String key, Object value) {
        return Jwts.builder()
                .claim(key, value)
                .setExpiration(new Date(System.currentTimeMillis() + expiredTime))
                .signWith(SignatureAlgorithm.HS256, sign)
                .compact();
    }

    public static Map<String, Object> paresToken(String token) {
        return (Map<String, Object>) Jwts.parser()
                .setSigningKey(sign)
                .parse(token)
                .getBody();
    }
}
